import {defineStore} from 'pinia';
import {getBorrowList, returnBorrow, exportBorrow} from '@/api/index.js';
import {ElMessage} from "element-plus"; // 引入API

export const adminBorrowManage = defineStore('adminBorrowManage', {
    state: () => ({
        borrowList: [], // 借用记录列表
        total: 0,       // 总记录数
        loading: false, // 加载状态
        queryParams: { // 查询参数
            userName: '',
            assetName: '',
            borrowStatus: '',
            pageNum: 1,
            pageSize: 10
        }
    }),
    getters: {},
    actions: {
        // 获取借用记录列表
        async getBorrowList() {
            this.loading = true;
            try {
                const response = await getBorrowList(this.queryParams);
                if (response.code === 200) {
                    this.borrowList = response.data.records;
                    this.total = response.data.total;
                } else {
                    ElMessage.error({message: '获取借用记录列表失败', duration: 3000});
                    console.error('获取借用记录列表失败:', response.message || '未知错误');
                }
            } catch (error) {
                ElMessage.error({message: '获取借用记录列表失败', duration: 3000});
                console.error('获取借用记录列表异常:', error);
            } finally {
                this.loading = false;
            }
        },
        // 归还资产
        async returnBorrow(borrowId) {
            try {
                const response = await returnBorrow(borrowId);
                if (response.code === 200) {
                    ElMessage.success({message: '归还成功', duration: 3000});
                    return true; // 返回成功
                } else {
                    ElMessage.error({message: response.message || '归还失败', duration: 3000});
                    return false; // 返回失败
                }
            } catch (error) {
                ElMessage.error({message: '归还失败', duration: 3000});
                console.error('归还异常:', error);
                return false; // 返回失败
            }
        },
        // 获取所有借用记录 (用于导出)
        async getAllBorrowList(query) {
            try {
                const response = await exportBorrow(query);
                if (response.code === 200) {
                    return response.data; // 返回所有数据
                } else {
                    ElMessage.error({message: '获取导出数据失败', duration: 3000});
                    console.error('获取导出数据失败:', response.message || '未知错误');
                    return {rows: [], total: 0}; // 返回空数据
                }
            } catch (error) {
                ElMessage.error({message: '获取导出数据失败', duration: 3000});
                console.error('获取导出数据异常:', error);
                return {rows: [], total: 0}; // 返回空数据
            }
        },
        // 设置查询参数
        setQueryParam(key, value) {
            this.queryParams[key] = value;
        },
        // 重置查询参数
        resetQueryParams() {
            this.queryParams = {
                userName: '',
                assetName: '',
                borrowStatus: '',
                pageNum: 1,
                pageSize: 10
            };
        },
        // 设置当前页码
        setPageNum(pageNum) {
            this.queryParams.pageNum = pageNum;
        },
        // 设置每页条数
        setPageSize(pageSize) {
            this.queryParams.pageSize = pageSize;
        }
    }
});